#include "blockstream.hpp"

namespace vl
{

blockstream::blockstream(stream& b)
: iobase(b.release_devbuf()), cur_read_state(beginning)
{

}

blockstream::blockstream(device_buf* b)
: iobase(b), cur_read_state(beginning)
{

}

bool blockstream::get()
{
	if(cur_read_state == beginning)
	{
		int i;
		if(read_int32(i))
		{
			cur_read_state = read_header;
			block_len = i;
		}
	}
	else
	{
		if(read(block_len))
		{
			cur_read_state = beginning;
			return true;
		}
	}
	
	return false;
}

void blockstream::put(vl::buffer const& buf)
{
	write_int32(buf.size());
	write(&buf[0], buf.size());
}

}
